﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runtime = Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using SSISFromCode.Helpers;

namespace SSISFromCode
{

    internal class FixedWidthGenerator : FlatFileGenerator, IRequireSourceConnection, IRequireTransform
    {

        /// <summary>
        /// A "derived column" is what you use to trim.  It's just the component you can jam in the pipeline and run functions on the data columns with.
        /// </summary>
        private IDTSComponentMetaData100 _derivedColumn;
        private CManagedComponentWrapper _derivedColumnInstance;
        
        void IRequireSourceConnection.ConfigureSourceConnection()
        {
            base.ConfigureSourceConnection();
            _connectionSource.Properties[FLAT_FILE_PROPERTY_NAMES.FORMAT].SetValue( _connectionSource, FORMAT.FIXED_WIDTH_MANAGER );
        }

        protected override void CreateDataFlowComponents( Runtime.Application app )
        {
            base.CreateDataFlowComponents( app );
            CreateDerivedColumnComponent( app );
        }

        private void CreateDerivedColumnComponent( Runtime.Application app )
        {
            _derivedColumn = _pipe.ComponentMetaDataCollection.New();
            _derivedColumn.Name = COMPONENT_NAMES.DERIVED_COLUMN;
            _derivedColumn.ComponentClassID = app.PipelineComponentInfos[OBJECT_CREATION.DERIVED_COLUMN].CreationName;
        }

        protected override void CreateDesignInstances()
        {
            base.CreateDesignInstances();

            _derivedColumnInstance = _derivedColumn.Instantiate();
            _derivedColumnInstance.ProvideComponentProperties();
        }

        protected override void ConnectDataFlowSubComponents()
        {
            TransformationGeneratorHelper.ConnectDataFlowSubComponents( _pipe, _dataSource, _derivedColumn, _dataDest );
        }

        public void ConfigureDataTransform()
        {
            TransformationGeneratorHelper.ConfigureDataTransformation( _derivedColumn, _derivedColumnInstance, this );
        }

        protected override void DoActualMapping()
        {
            TransformationGeneratorHelper.MapTransformedColumns( _dataDest, _dataDestInstance );
        }

    }

}
